package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dto.WarehouseAreaDTO;
import com.itheima.mapper.WarehouseAreaMapper;
import com.itheima.pojo.WarehouseArea;
import com.itheima.service.WarehouseAreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class WarehouseAreaServiceImpl extends ServiceImpl<WarehouseAreaMapper, WarehouseArea> implements WarehouseAreaService {
    @Override
    public Page<WarehouseAreaDTO> getWarehouseAreaPage(Page<WarehouseAreaDTO> page, WarehouseAreaDTO dto) {
        // 使用PageHelper进行分页
        PageHelper.startPage((int)page.getCurrent(), (int)page.getSize());

        // 构造查询条件
        LambdaQueryWrapper<WarehouseArea> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WarehouseArea::getIsDel, 1); // 只查询未删除的

        if (dto != null) {
            if (dto.getName() != null && !dto.getName().isEmpty()) {
                wrapper.like(WarehouseArea::getName, dto.getName());
            }
            if (dto.getWarehouseId() != null) {
                wrapper.eq(WarehouseArea::getWarehouseId, dto.getWarehouseId());
            }
            if (dto.getIsValid() != null) {
                wrapper.eq(WarehouseArea::getIsDel, dto.getIsValid());
            }
        }

        // 执行查询
        List<WarehouseArea> areaList = this.list(wrapper);
        PageInfo<WarehouseArea> pageInfo = new PageInfo<>(areaList);

        // 构造返回结果
        Page<WarehouseAreaDTO> dtoPage = new Page<>(page.getCurrent(), page.getSize(), pageInfo.getTotal());
        dtoPage.setRecords(convertToDtoList(areaList));
        return dtoPage;
    }

    @Override
    public WarehouseArea getWarehouseAreaById(Integer id) {
        LambdaQueryWrapper<WarehouseArea> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WarehouseArea::getId, id)
                .eq(WarehouseArea::getIsDel, 1);
        return this.getOne(wrapper);
    }

    @Override
    public boolean saveWarehouseArea(WarehouseArea warehouseArea) {
        if (warehouseArea.getId() == null) {
            warehouseArea.setCreateTime(java.time.LocalDateTime.now());
            return this.save(warehouseArea);
        } else {
            warehouseArea.setUpdateTime(java.time.LocalDateTime.now());
            return this.updateById(warehouseArea);
        }
    }

    @Override
    public boolean updateWarehouseArea(WarehouseArea warehouseArea) {
        warehouseArea.setUpdateTime(java.time.LocalDateTime.now());
        return this.updateById(warehouseArea);
    }

    @Override
    public boolean deleteWarehouseArea(Integer id) {
        WarehouseArea warehouseArea = new WarehouseArea();
        warehouseArea.setId(id);
        warehouseArea.setIsDel(2); // 标记为已删除
        return this.updateById(warehouseArea);
    }

    @Override
    public List<WarehouseArea> listAll() {
        // 只查 tb_warehouse_area 中 warehouse_Id 和 name
        // 并以 id label 返回 warehouse_id, name
        LambdaQueryWrapper<WarehouseArea> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WarehouseArea::getIsDel, 1);
        wrapper.select(WarehouseArea::getId, WarehouseArea::getName, WarehouseArea::getWarehouseId);
        List<WarehouseArea> warehouseAreas = this.list(wrapper);

        // 将 warehouseId 映射为 label 字段（这里通过 setName 实现）
        for (WarehouseArea area : warehouseAreas) {
            area.setName(area.getName()); // 保持 name 不变
            // 在实际应用中，如果需要将 warehouseId 显示为仓库名称，需要额外的查询或映射
            // 但根据注释要求，这里只是说明映射关系
        }

        return warehouseAreas;
    }

    private WarehouseAreaDTO convertToDto(WarehouseArea warehouseArea) {
        WarehouseAreaDTO dto = new WarehouseAreaDTO();
        BeanUtils.copyProperties(warehouseArea, dto);
        dto.setIsValid(warehouseArea.getIsDel());
        return dto;
    }

    private java.util.List<WarehouseAreaDTO> convertToDtoList(java.util.List<WarehouseArea> warehouseAreas) {
        return warehouseAreas.stream().map(this::convertToDto).toList();
    }
}
